package com.paireach.bw.broadcastreceiver;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.TaskStackBuilder;
import android.text.TextUtils;
import android.widget.RemoteViews;

import com.paireach.bw.R;
import com.paireach.bw.activity.BackActivity;
import com.paireach.bw.activity.CommonActivity;
import com.paireach.bw.activity.KaActivity;
import com.paireach.bw.activity.NewDriverOrderActivity;
import com.paireach.bw.utils.LogUtil;
import com.tencent.bugly.beta.Beta;

import org.json.JSONObject;

import cn.jpush.android.api.JPushInterface;

/**
 * Created by ${cqc} on 2017/7/7.
 */

public class JpushReceiver extends BroadcastReceiver {


    @Override
    public void onReceive(Context context, Intent intent) {
        Bundle bundle = intent.getExtras();

        if (JPushInterface.ACTION_REGISTRATION_ID.equals(intent.getAction())) {
            LogUtil.d("JPush用户注册成功");

        } else if (JPushInterface.ACTION_MESSAGE_RECEIVED.equals(intent.getAction())) {
            LogUtil.d("接受到推送下来的自定义消息");
            String message = bundle.getString(JPushInterface.EXTRA_MESSAGE);
            String json = bundle.getString(JPushInterface.EXTRA_EXTRA);
            LogUtil.d("EXTRA_MESSAGE" + message);
            LogUtil.d("EXTRA_EXTRA=" + json);
            if ("请更新App".equals(message)) {
                Beta.checkUpgrade();
            } else {
//            "params":"{\"status\":\"200\",\"orderCode\":\"40792677\",\"type\":\"newOrder\"}"}
                String type = "";
                String transportCode = "";
                String orderCode = "";
                try {
                    JSONObject jsonObject = new JSONObject(json);
                    String params = jsonObject.getString("params");
                    JSONObject jsonObject2 = new JSONObject(params);
                    type = jsonObject2.getString("type");
                    String status = jsonObject2.getString("status");
                    transportCode = jsonObject2.getString("transportCode");
                    orderCode = jsonObject2.getString("orderCode");

                } catch (Exception e) {
                    e.printStackTrace();
                } finally {
                    if (TextUtils.equals("newOrder", type)) {//新单提醒
                        showNotification(context, message, orderCode, transportCode, true);
                    } else {//其它通知，比如修改司机
                        showNotification(context, message, "", transportCode, false);
                    }
                }
            }

        } else if (JPushInterface.ACTION_NOTIFICATION_RECEIVED.equals(intent.getAction())) {
            LogUtil.d("接受到推送下来的通知");

        } else if (JPushInterface.ACTION_NOTIFICATION_OPENED.equals(intent.getAction())) {
            LogUtil.d("用户点击打开了通知");
        } else {
            LogUtil.d("Unhandled intent - " + intent.getAction());
        }
    }


    private void showNotification(Context context, String message, String orderCode, String transportCode, boolean flag) {
        NotificationManager nm = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);

        RemoteViews bigContentView = new RemoteViews(context.getPackageName(), R.layout.notification_content_view);
        bigContentView.setTextViewText(R.id.tv_big_notification, message);

        //特殊Activity
//        Intent intent = new Intent(context, NewDriverOrderActivity.class);
//        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
//        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, intent, PendingIntent.FLAG_UPDATE_CURRENT);

        Notification.Builder builder = new Notification.Builder(context);
        builder.setContentTitle("百威我来运")
                .setContentText(message)
                .setSmallIcon(R.mipmap.ic_launcher)//设置小图标，4.x在右边，5.x在左边
                .setDefaults(Notification.DEFAULT_ALL)
                .setPriority(Notification.PRIORITY_HIGH)
                .setAutoCancel(true);
        //普通Activity
        if (flag) {
            Class myClass = null;
            if ("KA".equals(transportCode)) {
                myClass = KaActivity.class;
            } else if ("COMMON".equals(transportCode)) {
                myClass = CommonActivity.class;
            } else if ("BACK".equals(transportCode)) {
                myClass = BackActivity.class;
            }
            if (myClass != null) {
                Intent intent = new Intent(context, myClass);
                intent.putExtra("orderCode", orderCode);
                intent.putExtra("fromNotification",true);
                TaskStackBuilder taskBuilder = TaskStackBuilder.create(context)
                        .addParentStack(myClass)
                        .addNextIntent(intent);
                PendingIntent pendingIntent = taskBuilder.getPendingIntent(0, PendingIntent.FLAG_UPDATE_CURRENT);
                builder.setContentIntent(pendingIntent);
            }
        }

        Notification notification = builder.build();
        notification.bigContentView = bigContentView;
        nm.notify(2, notification);
    }

}